import myAxios from '@/utils/request'
import axios from "axios";
export function exportFile(options) {
  const {
    method,
    url,
    data = null
  } = options;
  return axios({
    method,
    url,
    data,
    responseType: "blob"
  }).then(res => {
    const link = document.createElement("a");
    const blob = new Blob([res.data], {
      type: res.headers["content-type"]
    });
    link.style.display = "none";
    link.href = URL.createObjectURL(blob);
    link.setAttribute(
      "download",
      decodeURIComponent(
        res.headers["content-disposition"].slice("attachment;filename=".length)
      )
    );
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
}
export class Base {
  constructor(url) {
    this.url = url
  }

  // 查询所有 查询条件自己写
  findAll(params) {
    return myAxios.request({
      url: `${this.url}`,
      method: 'get',
      params: params
    })
  }

  // 查询单条
  findById(id) {
    return myAxios.request({
      url: `${this.url}/${id}`,
      method: 'get'
    })
  }

  // 新增单条
  insertOne(params) {
    return myAxios.request({
      url: `${this.url}`,
      method: 'post',
      data: params
    })
  }

  // 新增多条
  insertMany(params) {
    return myAxios.request({
      url: `${this.url}/insertMany`,
      method: 'post',
      data: params
    })
  }

  // 更新单条
  updateOne(id, params) {
    return myAxios.request({
      url: `${this.url}/${id}`,
      method: 'put',
      data: params
    })
  }

  // 更新多条
  updateMany(params) {
    return myAxios.request({
      url: `${this.url}/updateMany`,
      method: 'put',
      data: params
    })
  }

  // 删除单条
  deleteOne(id) {
    return myAxios.request({
      url: `${this.url}/${id}`,
      method: 'delete'
    })
  }

  // 删除多条  [id1,id2,id3]
  deleteMany(ids) {
    return myAxios.request({
      url: `${this.url}/deleteMany`,
      method: 'delete',
      data: ids
    })
  }
}